﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

namespace TasksLog
{
    public enum LoaderType : int
    {
        Default = 0,
        Save = 1,
        Diagram = 2,
        Load = 3
    }
    public class Loader : IDisposable
    {
        Thread thread = null;
        public LoaderType Type = LoaderType.Default;
        Form ParentForm = null;
        public Loader(Form Parent, LoaderType Type = LoaderType.Default)
        {
            this.Type = Type;
            this.ParentForm = Parent;
        }
        public static void Run(LoaderType Type, Form Parent)
        {
            switch (Type)
            {
                case LoaderType.Default: Application.Run(new LoaderForm(Parent)); break;
                case LoaderType.Save   : Application.Run(new SaveLoaderForm()); break;
                case LoaderType.Diagram: Application.Run(new LoadLoaderForm()); break;
                case LoaderType.Load   : Application.Run(new LoadLoaderForm()); break;
            }     
        }
        public void Start()
        {
            try
            {
                if (thread == null || thread.ThreadState == System.Threading.ThreadState.Aborted)
                {
                    thread = new Thread(delegate() { Run(this.Type, this.ParentForm); });
                    thread.Start();
                }
            }
            catch { }
        }
        public void Stop()
        {
            if (thread != null && thread.ThreadState != System.Threading.ThreadState.Aborted)
            {
                Thread.Sleep(100);
                thread.Abort();
            }
        }
        public void Dispose()
        {
            Thread.Sleep(100);
            thread.Abort();
        }
    }
}
